package com.ruoyi.project.bas.basProducttype.controller;

import java.util.List;

import com.ruoyi.project.bas.basSupplier.domain.BasSupplier;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.bas.basProducttype.domain.BasProductType;
import com.ruoyi.project.bas.basProducttype.service.IBasProductTypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物料分类管理Controller
 * 
 * @author qxq
 * @date 2023-05-31
 */
@Controller
@RequestMapping("/bas/basProducttype")
public class BasProductTypeController extends BaseController
{
    private String prefix = "bas/basProducttype";

    @Autowired
    private IBasProductTypeService basProductTypeService;

    @GetMapping()
    public String basProducttype()
    {
        return prefix + "/basProducttype";
    }

    /**
     * 查询物料分类管理树列表
     */
    @PostMapping("/list")
    @ResponseBody
    public List<BasProductType> list(BasProductType basProductType)
    {
        List<BasProductType> list = basProductTypeService.selectBasProductTypeList(basProductType);
        return list;
    }

    /**
     * 导出物料分类管理列表
     */
    @Log(title = "物料分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BasProductType basProductType)
    {
        List<BasProductType> list = basProductTypeService.selectBasProductTypeList(basProductType);
        ExcelUtil<BasProductType> util = new ExcelUtil<BasProductType>(BasProductType.class);
        return util.exportExcel(list, "物料分类管理数据");
    }

    /**
     * 新增物料分类管理
     */
    @GetMapping(value = { "/add/{productTypeNo}", "/add/" })
    public String add(@PathVariable(value = "productTypeNo", required = false) String productTypeNo, ModelMap mmap)
    {
        if (StringUtils.isNotNull(productTypeNo))
        {
            mmap.put("basProductType", basProductTypeService.selectBasProductTypeByProductTypeNo(productTypeNo));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存物料分类管理
     */
    @Log(title = "物料分类管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BasProductType basProductType)
    {
        return toAjax(basProductTypeService.insertBasProductType(basProductType));
    }

    /**
     * 下载模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<BasProductType> util = new ExcelUtil<BasProductType>(BasProductType.class);
        return util.importTemplateExcel("物料分类信息");
    }

    /**
     * 导入数据
     */
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BasProductType> util = new ExcelUtil<BasProductType>(BasProductType.class);
        List<BasProductType> productTypeList = util.importExcel(file.getInputStream());

        String message = basProductTypeService.importSupplier(productTypeList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 修改物料分类管理
     */
    @GetMapping("/edit/{productTypeNo}")
    public String edit(@PathVariable("productTypeNo") String productTypeNo, ModelMap mmap)
    {
        BasProductType basProductType = basProductTypeService.selectBasProductTypeByProductTypeNo(productTypeNo);
        mmap.put("basProductType", basProductType);
        return prefix + "/edit";
    }

    /**
     * 修改保存物料分类管理
     */
    @Log(title = "物料分类管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BasProductType basProductType)
    {
        return toAjax(basProductTypeService.updateBasProductType(basProductType));
    }

    /**
     * 删除
     */
    @Log(title = "物料分类管理", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{productTypeNo}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("productTypeNo") String productTypeNo)
    {
        return toAjax(basProductTypeService.deleteBasProductTypeByProductTypeNo(productTypeNo));
    }

    /**
     * 选择物料分类管理树
     */
    @GetMapping(value = { "/selectBasProducttypeTree/{productTypeNo}", "/selectBasProducttypeTree/" })
    public String selectBasProducttypeTree(@PathVariable(value = "productTypeNo", required = false) String productTypeNo, ModelMap mmap)
    {
        if (StringUtils.isNotNull(productTypeNo))
        {
            mmap.put("basProductType", basProductTypeService.selectBasProductTypeByProductTypeNo(productTypeNo));
        }
        return prefix + "/tree";
    }

    /**
     * 加载物料分类管理树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = basProductTypeService.selectBasProductTypeTree();
        return ztrees;
    }
}
